package com.czl.module_storehouse.activity.remand.home;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.czl.base.binding.viewadapter.flycoTabLayout.TabEntity;
import com.czl.base.config.AppConstants;
import com.czl.module_base.activity.BaseTabActivity;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.remand.damage.DamageRecordActivity;
import com.czl.module_storehouse.activity.remand.direct.DirectReturnActivity;
import com.czl.module_storehouse.activity.remand.selectedgoods.RemandSelectedGoodsActivity;
import com.czl.module_storehouse.event.RemandEvent;
import com.czl.module_storehouse.fragment.RemandHomeFragment;
import com.flyco.tablayout.listener.CustomTabEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import me.yokeyword.fragmentation.SupportFragment;

/**
 * @author xubin
 */
@Route(path = AppConstants.Router.Service.F_SERVICE_REMAND_HOME)
public class RemandHomeActivity extends BaseTabActivity {

    private List<SupportFragment> mFragmentList;

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("物品归还");
        EventBus.getDefault().register(this);
        View view = View.inflate(getContext(), R.layout.layout_remand_home_view, null);
        LinearLayout llRemand = view.findViewById(R.id.ll_remand);
        TextView textView = view.findViewById(R.id.text_scan);
        TextView selected = view.findViewById(R.id.text_selected);
        TextView tvDamageRecord = view.findViewById(R.id.tv_damage_record);
        textView.setOnClickListener(v -> {
            //扫码归还
            goRemandScanActivity();
        });
        selected.setOnClickListener(v -> {
            //选择物品归还
            goRemandSelectedGoodsActivity();
        });
        tvDamageRecord.setOnClickListener(v -> {
            //查看损坏记录
            nextDamageRecordActivity();
        });
        binding.viewPager2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                llRemand.setVisibility(i % 2 == 0 ? View.VISIBLE : View.GONE);
                tvDamageRecord.setVisibility(i % 2 == 1 ? View.VISIBLE : View.GONE);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    @Override
    protected List<SupportFragment> fragments() {
        if (mFragmentList == null) {
            mFragmentList = new ArrayList<>();
            mFragmentList.add(RemandHomeFragment.newInstance(0));
            mFragmentList.add(RemandHomeFragment.newInstance(1));
            mFragmentList.add(RemandHomeFragment.newInstance(2));
            mFragmentList.add(RemandHomeFragment.newInstance(3));
        }
        return mFragmentList;
    }

    @Override
    protected ArrayList<CustomTabEntity> mTabEntities() {
        ArrayList<CustomTabEntity> entities = new ArrayList<>();
        entities.add(new TabEntity("领用待还", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("领用已还", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("借用待还", R.mipmap.ic_add, R.mipmap.ic_add));
        entities.add(new TabEntity("借用已还", R.mipmap.ic_add, R.mipmap.ic_add));
        return entities;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void event(RemandEvent remandEvent) {
        if (remandEvent != null) {
            if (RemandEvent.RECEIVE == remandEvent.getTag()) {
                new Handler(Looper.myLooper()).postDelayed(() -> {
                    SupportFragment supportFragment0 = fragments().get(0);
                    SupportFragment supportFragment1 = fragments().get(1);
                    if (supportFragment0 instanceof RemandHomeFragment) {
                        ((RemandHomeFragment) supportFragment0).loadData(true);
                    }
                    if (supportFragment1 instanceof RemandHomeFragment) {
                        ((RemandHomeFragment) supportFragment1).loadData(true, 1);
                    }
                    binding.viewPager2.setCurrentItem(1, true);
                }, 200);

            } else if (RemandEvent.BORROW == remandEvent.getTag()) {
                new Handler(Looper.myLooper()).postDelayed(() -> {
                    SupportFragment supportFragment2 = fragments().get(2);
                    SupportFragment supportFragment3 = fragments().get(3);
                    if (supportFragment2 instanceof RemandHomeFragment) {
                        ((RemandHomeFragment) supportFragment2).loadData(true);
                    }
                    if (supportFragment3 instanceof RemandHomeFragment) {
                        ((RemandHomeFragment) supportFragment3).loadData(true, 3);
                    }
                    binding.viewPager2.setCurrentItem(3, true);
                }, 200);
            }
        }
    }

    private void nextDamageRecordActivity() {
        Intent intent = new Intent(getContext(), DamageRecordActivity.class);
        startActivity(intent);
    }

    private void goRemandScanActivity() {
        Intent intent = new Intent(getContext(), DirectReturnActivity.class);
        startActivity(intent);
    }

    private void goRemandSelectedGoodsActivity() {
        Intent intent = new Intent(getContext(), RemandSelectedGoodsActivity.class);
        startActivity(intent);
    }
}
